<?php

namespace app\modules\Basic\models;

use Yii;
use app\modules\Manage\models\User;

/**
 * This is the model class for table "ai_customer".
 *
 * @property int $id
 * @property string $name 客户名称
 * @property int $status 1 正常 2禁止 
 * @property int $user_id 创建人ID
 * @property string $created_at 创建时间
 * @property string $updated_at
 */
class Customer extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'ai_customer';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['name'], 'required'],
            [['status', 'user_id'], 'integer'],
            [['created_at', 'updated_at'], 'safe'],
            [['name'], 'string', 'max' => 255],
        ];
    }

    // 保存前处理
    public function beforeSave($insert) {//{{{

        if($insert){
            $this->created_at = date("Y-m-d H:i:s");
            $this->user_id    = Yii::$app->user->id;
        }
        

        return parent::beforeSave($insert);
    }//}}}

     //获取录入人
    public function getUser(){
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }

    // 获取所有有效客户
    public static function all(){//{{{
        
        $model = Customer::find()->where(['status'=>1])->all();     
        
        return $model;  
    }//}}}

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '客户名称',
            'status' => 'Status',
            'user_id' => '创建人',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }
}
